// Promise封装请求   options是一个对象里面的属性有url, data, method, load
//url 对应请求路径  data对象请求的参数  method对应请求方法  load对应是否显示加载中 1显示 0不显示
let util = require('./storage');
import JSEncrypt from './JSEncrypt'
//加密JSEncrypt
function encryptedData(data) {
    const jsencrypt = new JSEncrypt()
    jsencrypt.setPublicKey(util.get('publicKey')) //设置公钥
    return jsencrypt.encryptUnicodeLong(data)
}

function changeParam(param) {
    return JSON.stringify(param).replace(/:/g, '=').replace(/,/g, '&').replace(/{/g, '').replace(/}/g, '').replace(/"/g, '');
}

function httpReq(options) {
    if (options.load == 1) {
        wx.showLoading({
            title: '加载中',
            mask: true,
        })
    }
    let token = util.get('token')
    let header = {
        "Content-Type": "application/x-www-form-urlencoded",
        "Platform": "WeChat_Web",
        "showType": util.get('showType') || 1,
    }
    if (options.method == "POST") {
        header['Accept'] = "application/json;"
        header['Content-Type'] = "application/json; charset=utf-8"
    }

    if (token) {
        header['Authorization'] = token;
    }
    return new Promise((resolve, reject) => {
        if (options.data) {
            if (options.isJSEncrypt != false) {
                if (options.method == "POST") {
                    options.data = JSON.stringify(options.data)
                    var params = { EncryptionString: encryptedData(options.data) }
                    options.data = JSON.stringify(params)
                } else {
                    options.data = changeParam(options.data)
                    var params = { EncryptionString: encryptedData(options.data) }
                    options.data = changeParam(params)
                    options.url = options.url + '?' + options.data
                }
            }
        } else {
            options.data = {}

        }

        wx.request({
            url: options.url,
            header: header,
            data: options.data,
            method: options.method,
            complete: function (i) {
                wx.hideLoading()
            },
            success: function (res) {
                if (res.data.StatusCode === 200) {
                    resolve(res.data); //把请求到的数据发到引用请求的地方
                } else if (res.data.StatusCode === 401) {
                    wx.showModal({
                        title: '温馨提示',
                        content: res.data.ErrMsg,
                        showCancel: false,
                        confirmText: '我知道啦',
                        success: () => {
                            wx.redirectTo({
                                url: '/pages/login/index',
                            })
                        }
                    })
                } else if (res.data.StatusCode === -1) {
                    if(!options.Tips1){
                        wx.showModal({
                            title: '温馨提示',
                            content: res.data.ErrMsg,
                            showCancel: false,
                            confirmText: '我知道啦',
                            success: () => {
                                resolve(res.data);
                            }
                        })
                    }else{
                        resolve(res.data);
                    }
                   
                } else {
                    resolve(res.data);
                }
            },
            fail: function (err) {

                reject(err)
            },

        })
    });
}

function uploadReq(options) {
    let token = util.get('token')
    wx.showLoading({
        title: '上传中',
        mask: true,
    })
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            url: options.url,
            filePath: options.filePath,
            name: 'file',
            formData: options.data,
            header: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Authorization": token,
                "showType": util.get('showType') || 1,
                "Platform": "WeChat_Web",
            },
            complete: gcr => {
                wx.hideLoading()
            },
            success: res => {
                if (res.statusCode == 200) {
                    if (res.errMsg == 'uploadFile:ok') {
                        resolve(JSON.parse(res.data))
                    } else {
                        resolve(res)
                    }
                } else { resolve(res) }
            },
            fail: err => {
                reject(err)
            },
        })
    })

}

module.exports = {
    httpReq,
    uploadReq
}